<?php

namespace Common\Model;
use think\Model;

class ImgcateModel extends BaseModel{

	//追加属性
    public $append = [
        'child_num',        //子分类条数
        'type_name',        //分类类型
        'p_name',        //分类类型
        'child_list',//子分类列表
	];

    
    //子分类数量
    function getChildListAttr($val, $data){
        $list =[];
        if($data['id']){
            $model = M('Imgcate');
            $where=[];
            $where['pid']=$data['id'];
            $where['is_delete']=0;
            $list = $model->where($where)->field('id,title')->order('id asc')->select();
            foreach($list as $key=>$val){
                $where2['pid'] = $val['id'];
                $where2['is_delete'] = 0;
                $list2 = $model->where($where2)->field('id,title')->order('id asc')->select() ? : [];
                $list[$key]['child_list'] = $list2;
            }
        }
        return $data['child_list'] = $list;
    }
    //子分类数量
    function getPnameAttr($val, $data){
        $text = '';
        if($data['pid']){
            $model = M('Imgcate');
            $text = $model->where('id='.$data['pid'])->getField('title');
        }
        return $text;
    }

    //子分类数量
    function getChildNumAttr($val, &$data){
        $count = 0;
        if($data['status']!=1){
            $wdata['status'] = $data['status'];
            $wdata['pid'] = $data['id'];
            $count = $this->where($wdata)->count();
        }
        if($data['status']==3){
            //规格
            $spec_wdata['status'] = 1;
            $spec_wdata['pid'] = $data['id'];
            $spec_num = $this->where($spec_wdata)->count() ? : 0;
            $data['spec_num'] = $spec_num;
        }
        return $count;
    }

    //分类类型
    function getTypeNameAttr($val, $data){
        $array = [
            0=>'产品部件分类', 
            1=>'规格信息分类',
            2=>'主辅料信息分类',
            3=>'设计师款分类',
            4=>'特殊体型分类',
            10=>'其他分类',
        ];
        $text = $array[$data['status']];
        return $text ? : '';
    }

    //搜索
    function _before_search(){
        $map = [];
        if(I('keyword')){
            $map['title'] = array('like','%'.I('keyword').'%');
        }
        if(I('sid')){
            $map['gNote'] = I('sid');
        }
        $map['is_delete'] = I('is_delete') ? : 0;
        $user_msg = $this->uinfo;;
        $map['uid'] = $user_msg['uid'];
        return $map;
    }

    //规格信息
    function getGuigeList($pid){
        $model = D('Imgcate');
        $wdata['pid'] = $pid;
        $list = $model->where($wdata)->select() ? : [];
        return $list;
    }

    //获取全部子分类
    function getChildIds($id){
        $ids = [];
        $model = M('Imgcate');
        $wdata['pid'] = $id;
        $wdata['is_delete'] = 0;
        $list = $model->where($wdata)->select();
        if($list){
            foreach($list as $val){
                $ids[] = $val['id'];
                $new_ids = $this->getChildIds($val['id']);
                $ids = array_merge($ids,$new_ids);
            }
        }
        return $ids;
    }



}